package security.diffie.hellmann;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * Permet de générer des nombres premiers aléatoire, utilise le multi-threading.
 * @author Jean-François Augé
 */
public class Prime {
	
	/**
	 * Génére un nombre premier aléatoire.
	 * @param cores
	 * @param size
	 * @return
	 * @throws InterruptedException
	 */
	@SuppressWarnings("static-access")
	public static BigInteger generatePrime(int cores, int size) throws InterruptedException {
		int winner;
		List<PrimeThread> l = new ArrayList<PrimeThread>();
		for(int i=0; i<cores; i++) {
			PrimeThread pg = new PrimeThread(i, size);
			l.add(pg);
			pg.start();
		}
		l.get(0).join();

		winner = l.get(0).getWinner();
		return l.get(winner).getB();
	}
	
	
	// tests
	public static void main(String[] args) {
		BigInteger b = null;
		try {
			long start = System.currentTimeMillis();
			b = Prime.generatePrime(4, 512);
			System.out.println(System.currentTimeMillis()-start+" ms");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(b);
	}

}
